﻿using System;
using System.Globalization;
using TS3QueryLib.Core.CommandHandling;
using TS3QueryLib.Core.Common;

namespace TS3QueryLib.Core.Server.Entities
{
    public class LogEntry : LogEntryLight, IDump
    {
        #region Properties

        public DateTime? TimeStamp { get; protected set; }
        public string RawMessage { get; protected set; }
        public string LogCategory { get; protected set; }

        #endregion

        #region Constructor

        private LogEntry()
        {
            
        }

        #endregion

        #region Public Methods

        public static LogEntry Parse(CommandParameterGroup currentParameterGroup, CommandParameterGroup firstParameterGroup)
        {
            if (currentParameterGroup == null)
                throw new ArgumentNullException("currentParameterGroup");

            LogEntry logEntry = new LogEntry {RawMessage = currentParameterGroup.GetParameterValue("l")};
            string[] rawMessageParts = logEntry.RawMessage.Split('|');

            DateTime timeStamp;

            if (rawMessageParts.Length > 0 && DateTime.TryParse(rawMessageParts[0], CultureInfo.InvariantCulture, DateTimeStyles.None, out timeStamp))
                logEntry.TimeStamp = timeStamp;

            logEntry.LogLevel = LogLevel.Info;

            if (rawMessageParts.Length > 1)
                logEntry.LogLevel = (LogLevel) Enum.Parse(typeof (LogLevel), rawMessageParts[1].Trim(), true);

            if (rawMessageParts.Length > 2)
                logEntry.LogCategory = rawMessageParts[2].Trim();

            if (rawMessageParts.Length > 4)
                logEntry.Message = rawMessageParts[4].TrimStart();


            return logEntry;
        }

        #endregion
    }
}